import 'package:flutter/material.dart';

//Flutter 富文本
void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    //脚手架
    return Scaffold(
      appBar: AppBar(
        //中间的标题
        title: Text('这里是中间'),
        //标题剧中
        centerTitle: true,
      ),
      body: Center(
        child: SizedBox.expand(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              SizedBox(
                width: 200,
                height: 100,
                child: PageView(
                  children: [
                    Container(width: 200, height: 100, color: Colors.green),
                    Container(width: 200, height: 100, color: Colors.yellow),
                    Container(width: 200, height: 100, color: Colors.red),
                  ],
                ),
              ),

              SizedBox(height: 30),

              SizedBox(
                width: 200,
                height: 100,
                child: PageView.builder(
                  itemCount: 100,
                  itemBuilder: (BuildContext context, int index) {
                    return Container(
                      child: Text("第$index个View"),
                      color: Colors.grey,
                    );
                  },
                ),
              ),

              SizedBox(height: 30),

              SizedBox(
                width: double.infinity,
                height: 300,
                child: Stack(
                  children: [
                    Positioned.fill(
                      child: PageView.builder(
                        onPageChanged: (int index) {
                          //记录一下这个页面
                          _currentPageIndex = index;
                          setState(() {});
                        },
                        itemCount: _imageList.length,
                        itemBuilder: (BuildContext context, int index) {
                          String img = _imageList[index];
                          return Container(
                            child: Image.asset(img, fit: BoxFit.fill),
                            color: Colors.grey,
                          );
                        },
                      ),
                    ),

                    Positioned(
                      child: Container(
                        padding: EdgeInsets.only(left: 4, right: 4),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(10),
                          color: Colors.white.withValues(alpha: 0.5),
                        ),
                        child: Text(
                          '${_currentPageIndex + 1}/${_imageList.length}',
                        ),
                      ),
                      right: 20,
                      bottom: 20,
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
      // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  int _currentPageIndex = 0;
  List<String> _imageList = [
    'asset/images/s01.png',
    'asset/images/s02.png',
    'asset/images/s03.png',
    'asset/images/s04.png',
  ];
}
